#!/usr/bin/env python3
"""
OBS API使用示例
演示如何通过HTTP POST调用OBS控制接口
"""

import requests
import json


def test_obs_api():
    """测试OBS API的各种功能"""
    
    # API服务器地址
    base_url = "http://localhost:7866"
    
    # OBS连接配置
    obs_config = {
        "host": "localhost",
        "port": 4455,
        "password": "SbVnuIEYVFoyPW3Y",  # 修改为你的密码
        "default_scene": "test",  # 修改为你的场景名
        "preferred_media_sources": ["媒体源", "视频1", "视频2"]  # 修改为你的媒体源名称
    }
    
    print("=== OBS API测试 ===\n")
    
    # 1. 健康检查
    print("1. 健康检查")
    try:
        response = requests.get(f"{base_url}/health")
        print(f"状态: {response.status_code}")
        print(f"响应: {response.json()}")
    except Exception as e:
        print(f"错误: {e}")
    print()
    
    # 2. 获取场景列表
    print("2. 获取场景列表")
    try:
        response = requests.post(f"{base_url}/api/obs/scenes", json=obs_config)
        print(f"状态: {response.status_code}")
        result = response.json()
        print(f"响应: {json.dumps(result, ensure_ascii=False, indent=2)}")
    except Exception as e:
        print(f"错误: {e}")
    print()
    
    # 3. 获取媒体源列表
    print("3. 获取媒体源列表")
    try:
        request_data = obs_config.copy()
        request_data["command"] = "sources"
        
        response = requests.post(f"{base_url}/api/obs/control", json=request_data)
        print(f"状态: {response.status_code}")
        result = response.json()
        print(f"响应: {json.dumps(result, ensure_ascii=False, indent=2)}")
        
        # 获取第一个媒体源作为测试目标
        if result.get("success") and result.get("media_sources"):
            test_source = result["media_sources"][0]
            print(f"将使用媒体源进行测试: {test_source}")
            
            # 4. 播放视频
            print("\n4. 播放视频")
            play_data = obs_config.copy()
            play_data.update({
                "command": "play",
                "source_name": test_source
            })
            
            response = requests.post(f"{base_url}/api/obs/control", json=play_data)
            print(f"状态: {response.status_code}")
            print(f"响应: {json.dumps(response.json(), ensure_ascii=False, indent=2)}")
            
            # 5. 获取媒体状态
            print("\n5. 获取媒体状态")
            status_data = obs_config.copy()
            status_data.update({
                "command": "status",
                "source_name": test_source
            })
            
            response = requests.post(f"{base_url}/api/obs/control", json=status_data)
            print(f"状态: {response.status_code}")
            print(f"响应: {json.dumps(response.json(), ensure_ascii=False, indent=2)}")
            
            # 6. 暂停视频
            print("\n6. 暂停视频")
            pause_data = obs_config.copy()
            pause_data.update({
                "command": "pause",
                "source_name": test_source
            })
            
            response = requests.post(f"{base_url}/api/obs/control", json=pause_data)
            print(f"状态: {response.status_code}")
            print(f"响应: {json.dumps(response.json(), ensure_ascii=False, indent=2)}")
            
            # 7. 隐藏视频源
            print("\n7. 隐藏视频源")
            hide_data = obs_config.copy()
            hide_data.update({
                "command": "hide",
                "source_name": test_source
            })
            
            response = requests.post(f"{base_url}/api/obs/control", json=hide_data)
            print(f"状态: {response.status_code}")
            print(f"响应: {json.dumps(response.json(), ensure_ascii=False, indent=2)}")
            
            # 8. 显示视频源
            print("\n8. 显示视频源")
            show_data = obs_config.copy()
            show_data.update({
                "command": "show",
                "source_name": test_source
            })
            
            response = requests.post(f"{base_url}/api/obs/control", json=show_data)
            print(f"状态: {response.status_code}")
            print(f"响应: {json.dumps(response.json(), ensure_ascii=False, indent=2)}")
        
    except Exception as e:
        print(f"错误: {e}")
    print()
    
    # 9. 断开连接
    print("9. 断开OBS连接")
    try:
        response = requests.post(f"{base_url}/api/obs/disconnect")
        print(f"状态: {response.status_code}")
        print(f"响应: {response.json()}")
    except Exception as e:
        print(f"错误: {e}")


def example_curl_commands():
    """生成curl命令示例"""
    
    print("\n=== cURL命令示例 ===\n")
    
    obs_config = {
        "host": "localhost",
        "port": 4455,
        "password": "your_password",
        "default_scene": "your_scene",
        "preferred_media_sources": ["媒体源1", "媒体源2"]
    }
    
    examples = [
        {
            "title": "获取场景列表",
            "url": "/api/obs/scenes",
            "data": {k: v for k, v in obs_config.items() if k in ["host", "port", "password"]}
        },
        {
            "title": "获取媒体源列表",
            "url": "/api/obs/control",
            "data": {**obs_config, "command": "sources"}
        },
        {
            "title": "播放视频",
            "url": "/api/obs/control", 
            "data": {**obs_config, "command": "play", "source_name": "媒体源"}
        },
        {
            "title": "暂停视频",
            "url": "/api/obs/control",
            "data": {**obs_config, "command": "pause", "source_name": "媒体源"}
        },
        {
            "title": "获取媒体状态",
            "url": "/api/obs/control",
            "data": {**obs_config, "command": "status", "source_name": "媒体源"}
        }
    ]
    
    for example in examples:
        print(f"# {example['title']}")
        print("curl -X POST http://localhost:7866" + example['url'] + " \\")
        print("  -H 'Content-Type: application/json' \\")
        print("  -d '" + json.dumps(example['data'], ensure_ascii=False) + "'")
        print()


if __name__ == "__main__":
    print("请确保API服务器正在运行: python api_server.py")
    print("然后运行此脚本进行测试\n")
    
    choice = input("选择测试方式 (1=Python测试, 2=显示curl命令, 3=两者): ")
    
    if choice in ['1', '3']:
        test_obs_api()
    
    if choice in ['2', '3']:
        example_curl_commands()